package com.magicborrow.view;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.magicborrow.R;
import com.magicborrow.huanxin.ChatActivity;
import com.magicborrow.utils.ViewPressedUtil;

/**
 * Created by zhoul on 2016/7/23.
 */
public class ContactDialog extends Dialog implements View.OnClickListener {
    private String userName;
    private int userId;
    private String phoneNumber;
    private String avatar;


    public ContactDialog(Context context, String userName, int userId, String phoneNumber, String avatar) {
        super(context, R.style.DialogStyleBottom);
        this.userId = userId;
        this.userName = userName;
        this.phoneNumber = phoneNumber;
        this.avatar = avatar;

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.contact_dialog);
        findViewById(R.id.tv_http_msg).setOnClickListener(this);
        findViewById(R.id.tv_http_msg).setOnTouchListener(ViewPressedUtil.TouchPress);
        findViewById(R.id.tv_tell).setOnClickListener(this);
        findViewById(R.id.tv_tell).setOnTouchListener(ViewPressedUtil.TouchPress);
        findViewById(R.id.tv_short_msg).setOnClickListener(this);
        findViewById(R.id.tv_short_msg).setOnTouchListener(ViewPressedUtil.TouchPress);
        findViewById(R.id.tv_cache).setOnClickListener(this);
        findViewById(R.id.tv_cache).setOnTouchListener(ViewPressedUtil.TouchPress);
        TextView tvName = (TextView) findViewById(R.id.tv_username);
        TextView tvTell = (TextView) findViewById(R.id.tv_phone_number);
        tvName.setText(userName);
        tvTell.setText("tell:" + phoneNumber);
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case R.id.tv_tell:
                intent = new Intent(Intent.ACTION_DIAL);
                intent.setData(Uri.parse("tel:" + phoneNumber));
                if (intent.resolveActivity(getContext().getPackageManager()) != null) {
                    getContext().startActivity(intent);
                }
                break;
            case R.id.tv_short_msg:
                Uri smsToUri = Uri.parse("smsto:" + phoneNumber);
                intent = new Intent(Intent.ACTION_SENDTO, smsToUri);
                getContext().startActivity(intent);
                break;
            case R.id.tv_cache:
                dismiss();
                break;
            case R.id.tv_http_msg:
                getContext().startActivity(new Intent(getContext(), ChatActivity.class).putExtra("chatType", 1)
                        .putExtra("userId", userId + "")
                        .putExtra("username", userName)
                        .putExtra("avatarPath", avatar));
                break;
        }
    }

}

